package com.ruoyi.web.controller.project.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.controller.project.domain.TOrderRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.controller.project.domain.TVehicle;
import com.ruoyi.web.controller.project.service.ITVehicleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 车辆信息Controller
 * 
 * @author ruoyi
 * @date 2025-06-27
 */
@Slf4j
@RestController
@RequestMapping("/project/vehicle")
public class TVehicleController extends BaseController
{
    @Autowired
    private ITVehicleService tVehicleService;



    @Log(title = "车辆信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport, Long userId) throws Exception
    {
        ExcelUtil<TVehicle> util = new ExcelUtil<TVehicle>(TVehicle.class);
        List<TVehicle> tVehicleList = util.importExcel(file.getInputStream());
        log.debug("tVehicleList:{}", tVehicleList);
        String operName = getUsername();
        String message = "";
        try {
            message = tVehicleService.importVehicleList(tVehicleList, updateSupport, operName,userId);
        } catch (Exception e) {
            log.debug(e.getMessage());
        }

        return success(message);
    }

    /**
     * 查询车辆信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TVehicle tVehicle)
    {
        startPage();
        Long userId = SecurityUtils.getUserId();
        if (!userId.equals(1L) && !userId.equals(102L)){
            tVehicle.setUserId(userId);
        }
        List<TVehicle> list = tVehicleService.selectTVehicleList(tVehicle);
        return getDataTable(list);
    }

    /**
     * 导出车辆信息列表
     */
    @Log(title = "车辆信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TVehicle tVehicle)
    {
        Long userId = SecurityUtils.getUserId();
        if (!userId.equals(1L) && !userId.equals(102L)){
            tVehicle.setUserId(userId);
        }
        List<TVehicle> list = tVehicleService.selectTVehicleList(tVehicle);
        ExcelUtil<TVehicle> util = new ExcelUtil<TVehicle>(TVehicle.class);
        util.exportExcel(response, list, "车辆信息数据");
    }

    /**
     * 获取车辆信息详细信息
     */
    @GetMapping(value = "/{vehicleId}")
    public AjaxResult getInfo(@PathVariable("vehicleId") String vehicleId)
    {
        return success(tVehicleService.selectTVehicleByVehicleId(vehicleId));
    }

    /**
     * 新增车辆信息
     */
    @Log(title = "车辆信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TVehicle tVehicle)
    {
        return toAjax(tVehicleService.insertTVehicle(tVehicle));
    }

    /**
     * 修改车辆信息
     */
    @Log(title = "车辆信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TVehicle tVehicle)
    {
        return toAjax(tVehicleService.updateTVehicle(tVehicle));
    }

    /**
     * 删除车辆信息
     */
    @Log(title = "车辆信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{vehicleIds}")
    public AjaxResult remove(@PathVariable String[] vehicleIds)
    {
        return toAjax(tVehicleService.deleteTVehicleByVehicleIds(vehicleIds));
    }
}
